package com.ego.item.controller;

import com.ego.item.entity.Category;
import com.ego.item.service.CategoryService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResponseEntity<List<Category>> list(@RequestParam("pid") Long pid) {
        List<Category> result = categoryService.queryCategoryListByParentId(pid);
        if (result == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Long> add(@RequestBody Category category){
        categoryService.save(category);
        return ResponseEntity.ok(category.getId());
    }

    @PutMapping
    public ResponseEntity<Void> update(@RequestParam("id") Long id,@RequestParam("name") String name){
        categoryService.update(id,name);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id){
        categoryService.delete(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过品牌id查询商品分类
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> list = this.categoryService.queryByBrandId(bid);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }

    @GetMapping("/list/ids")
    public ResponseEntity<List<Category>> queryListByIds(@RequestParam("ids") List<Long> ids){
        List<Category> result = categoryService.findListByIds(ids);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/list/cids")
    ResponseEntity<List<Category>> queryListByIdList(@RequestParam("cids") List<Long> categoryIdList) {
        return ResponseEntity.ok(categoryService.findListByIdList(categoryIdList));
    }

}
